package com.koxudaxi.poetry


import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.module.Module
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.openapi.util.NlsSafe
import com.intellij.util.ui.FormBuilder
import org.jetbrains.annotations.SystemDependent
import java.awt.BorderLayout
import javax.swing.JPanel

class PyAddNewPoetryFromFilePanel(private val module: Module) : JPanel() {

    val envData: Data
        get() = Data(poetryPathField.text)

    private val poetryPathField = TextFieldWithBrowseButton()

    init {
        poetryPathField.apply {
            getPoetryExecutable()?.absolutePath?.also { text = it }

            addBrowseFolderListener(
                    "Select Path to Poetry Executable",
                    null,
                    module.project,
                    FileChooserDescriptorFactory.createSingleFileOrExecutableAppDescriptor()
            )
        }

        layout = BorderLayout()
        val formPanel = FormBuilder.createFormBuilder()
                .addLabeledComponent("Poetry executable:", poetryPathField)
                .panel
        add(formPanel, BorderLayout.NORTH)
    }

    fun validateAll(): List<ValidationInfo> = listOfNotNull( validatePoetryExecutable(poetryPathField.text))

    data class Data(val poetryPath: @NlsSafe @SystemDependent String)
}